<?

/*
*
*   Excalibur Content Management System
*   Copyright	© 2008 Egor "Sontan" Kuryanovich
*				© 2008 Anatoli "KnopiK" Konapelko
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/


header('Content-type: text/html; charset=utf-8');  
header("Cache-Control: no-cache");

$EXCMS = "work";

error_reporting (0);

include $_SERVER['DOCUMENT_ROOT']."/engine/settings.php";
include $_SERVER['DOCUMENT_ROOT']."/engine/mysql.class.php";
include $_SERVER['DOCUMENT_ROOT']."/engine/functions.php";
include $_SERVER['DOCUMENT_ROOT']."/modules/articles/settings.php";
$db = new DB($server, $dbusername, $dbpassword, $dbname);
$db->connect();

if (!$mainsettings = $db->query ("SELECT * FROM site_main")) { print "<center><b>Невозможно взять данные из БД!<br>Обратитесь к администратору сайта!</b>"; exit; }
$SITE = $db->fetch_array ($mainsettings);
$theme = $SITE['site_theme'];
if (isset($_COOKIE['login']) && isset ($_COOKIE['pass']) && $_COOKIE['login'] != "false" && $_COOKIE['pass'] != "false") {
	$GLOBAL_USER_LOGIN = addslashes($_COOKIE['login']);
	$GLOBAL_USER_PASS = addslashes($_COOKIE['pass']);
	if ($GLOBAL_USER_LOGIN != "" && $GLOBAL_USER_PASS != "")
		$SELECT_USER = $db->query ("SELECT * FROM ".DB_PEREFIX."_users WHERE user_login = '$GLOBAL_USER_LOGIN' && user_password = '$GLOBAL_USER_PASS'");
	if ($db->num_rows ($SELECT_USER) == 1) {
		$GLOBAL_USER = $db->fetch_array ($SELECT_USER);
	}
}

if (!isset($GLOBAL_USER)) exit;

$id = intval ($_POST['id']);
if ($_POST['value'] == 0) $bad = true;
if ($_POST['value'] == 1) $good = true;

$get_art = $db->query ("SELECT art_rating, art_voters FROM ".DB_PEREFIX."_articles WHERE art_id = '$id'");
if ($db->num_rows ($get_art) == 1) {
	$art = $db->fetch_array ($get_art);
	
	$vote = false;
	$voters = explode (",", $art['art_voters']);
	foreach ($voters as $v) if ($GLOBAL_USER['user_id'] == trim($v)) $vote = true;
	
	if ($art['art_voters'] == "") $new_voters = $GLOBAL_USER['user_id'];
	else $new_voters = $art['art_voters'].",".$GLOBAL_USER['user_id'];
	
	$rating = $art['art_rating'];
	if ($vote == false) {
		if ($bad == true) $rating -= $rating_unit;
		if ($good == true) $rating += $rating_unit;
		$db->query ("UPDATE ".DB_PEREFIX."_articles SET art_rating = '$rating', art_voters = '$new_voters' WHERE art_id = '$id'");
	}
	//print number_format($rating, 2, '.', '');
	($rating == 0) ? $rating = "0 баллов" : $rating = count_smth ($rating, array ("балл", "балла", "баллов"));
	print $rating;
}

